<template> 
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float: right"
          @click="searchBrandList()"
          type="primary"
          size="small">
          查询结果
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form :inline="true" :model="listQuery" size="small" label-width="140px">
          <el-form-item label="输入搜索：">
            <el-input style="width: 203px" v-model="listQuery.keyword" placeholder="科目名称/关键字"></el-input>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
      <el-button
        class="btn-add"
        @click="addSubject"
        size="mini">
        添加
      </el-button>
    </el-card>
    <div class="table-container">
      <el-table ref="brandTable"
                :data="list"
                style="width: 100%"
                @selection-change="handleSelectionChange"
                v-loading="listLoading"
                border>
        <el-table-column type="selection" width="60" align="center"></el-table-column>
        <el-table-column label="编号" width="100" align="center">
          <template slot-scope="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column label="科目名称" align="center">
          <template slot-scope="scope">{{ scope.row.name }}</template>
        </el-table-column>

        <el-table-column label="排序" width="100" align="center">
          <template slot-scope="scope">{{ scope.row.sort }}</template>
        </el-table-column>
        <el-table-column label="添加时间" width="160" align="center">
          <template slot-scope="scope">
            {{ scope.row.createdTime }}
          </template>
        </el-table-column>
        <el-table-column label="更新时间" width="160" align="center">
          <template slot-scope="scope">
            {{ scope.row.updatedTime }}
          </template>
        </el-table-column>

        <el-table-column label="操作" width="200" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleUpdate(scope.$index, scope.row)">编辑
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-dialog
        @close="closeDialogHandler"
        :title="isEdit?'编辑科目':'添加科目'"
        :visible.sync="dialogVisible"
        width="40%">
        <el-form :model="subject"
                 :rules="rules"
                 ref="adminForm"
                 label-width="150px" size="small">
          <el-form-item label="科目名称：" prop="name">
            <el-input v-model="subject.name"></el-input>
          </el-form-item>

          <el-form-item label="优先级" prop="sort">
            <el-input-number v-model="subject.sort" placeholder=""></el-input-number>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleDialogConfirm" size="small">确 定</el-button>
          <el-button @click="dialogVisible = false" size="small">取 消</el-button>
      </span>
      </el-dialog>
    </div>
    <div class="batch-operate-container">
      <el-select
        size="small"
        v-model="operateType" placeholder="批量操作">
        <el-option
          v-for="item in operates"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
      <el-button
        style="margin-left: 20px"
        class="search-button"
        @click="handleBatchOperate()"
        type="primary"
        size="small">
        确定
      </el-button>
    </div>

    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="listQuery.pageSize"
        :page-sizes="[5,10,15]"
        :current-page.sync="listQuery.pageNum"
        :total="total">
      </el-pagination>
    </div>

  </div>
</template>
<script>
import {deleteBatch, fetchList, deleteSubject, updateSubject, createSubject} from "@/api/subject"


const SubjectInit = {
  name: '',
  sort: 0
}
export default {
  name: 'brandList',
  data() {
    return {
      isEdit: false,
      dialogVisible: false,
      operates: [
        {
          label: "批量删除",
          value: "delSubjects"
        }
      ],
      operateType: "delSubjects",
      listQuery: {
        keyword: null,
        pageNum: 1,
        pageSize: 10
      },
      subject: Object.assign({}, SubjectInit),
      subId: 0,
      list: null,
      total: null,
      listLoading: true,
      multipleSelection: [],
      rules: {
        name: [
          {required: true, message: '请输入分类名称', trigger: 'change'}
        ]
      }
    }
  },
  created() {
    this.getList();
  },
  methods: {
    addSubject() {
      this.isEdit = false;
      this.dialogVisible = true;
    },
    handleDialogConfirm() {
      if (this.isEdit) {
        updateSubject(this.subId, this.subject).then(
          res => {
            this.$message.success(res.message)
            this.dialogVisible = false;
            this.getList();
          }
        )
      } else {
        createSubject(this.subject).then(
          res => {
            this.$message.success(res.message)
            this.subject = Object.assign({}, SubjectInit)
            this.dialogVisible = false;
            this.getList();
          }
        )
      }
    },
    getList() {
      this.listLoading = true;
      fetchList(this.listQuery).then(response => {
        this.listLoading = false;
        this.list = response.data.list;
        this.total = response.data.total;
        this.totalPage = response.data.totalPage;
        this.pageSize = response.data.pageSize;
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleUpdate(index, row) {

      this.subId = row.id;
      this.subject.name = row.name;
      this.subject.sort = row.sort;
      this.dialogVisible = true;
      this.isEdit = true
      debugger
    },
    handleDelete(index, row) {
      this.$confirm('是否要删除该科目', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteSubject(row.id).then(response => {
          this.$message({
            message: '删除成功',
            type: 'success',
            duration: 1000
          });
          this.getList();
        });
      });
    },
    handleSizeChange(val) {
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.getList();
    },
    searchBrandList() {
      this.listQuery.pageNum = 1;
      this.getList();
    },
    handleBatchOperate() {
      let ids = [];
      this.multipleSelection.forEach(item => {
        ids.push(item.id)
      });
      if (ids.length === 0) {
        this.$message.info("未选择删除对象")
        return;
      }
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteBatch(ids).then(
          res => {
            this.$message.success(res.message)
            this.getList()
          }
        )
      })


      // data.append("showStatus", showStatus);
      // updateShowStatus(data).then(response => {
      //   this.getList();
      //   this.$message({
      //     message: '修改成功',
      //     type: 'success',
      //     duration: 1000
      //   });
      // });
    },
    closeDialogHandler() {
      if (this.isEdit) {
        this.subject = Object.assign({}, SubjectInit)
      }
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>


</style>


